import {observable, action, runInAction} from 'mobx';
import http from '../../util/axios';
import APIS from '../../config/apis';

/*import { configure } from 'mobx';

// 启用严格模式 不允许在action之外修改state
configure({ enforceActions: "strict" });*/


class UserList {
    // 数据列表
    @observable public list: Table.Data[] = [];

    // 保留搜索状态
    @observable public searchStatus: Table.Values = {
        pageIndex: 1,
        pageSize: 6,
    };

    // 存放详情原始数据
    @observable public details: any = {};

    @observable public total: number = 1;

    // 是否在加载中
    @observable public listLoading: boolean = false;
    @observable public detailsLoading: boolean = false;

    // safecode的数据
    @observable public modalVisible: boolean = false;

    // 加载列表
    @action public getList = async (values: Table.Values) => {
        this.listLoading = true;

        const result = await http.get(APIS.USER_LIST, values);


        if (result.code !== 0) {
            runInAction(() => {
                this.listLoading = false;
            })
            return result.message;
        }
        runInAction(() => {
            const {data, total} = result.data;

            this.listLoading = false;
            this.list = data;
            this.total = total;
            this.searchStatus = values;
        })
    }

    // 加载详情
    @action public getDetails = async (id: string) => {
        this.detailsLoading = true;

        const result = await http.get(APIS.USER_DETAILS, {id});


        if (result.code !== 0) {
            runInAction(() => {
                this.detailsLoading = false;
            })
            return result.message;
        }



        runInAction(() => {
            this.detailsLoading = false;
            this.details = result.data;
        })
    }

    /**
     * 更新详情数据
     * @param values
     */
    @action public updateDetails = async (values: any) => {
        this.detailsLoading = true;

        const result = await http.post(APIS.USER_UPDATE, values);

        if (result.code !== 0) {
            runInAction(() => {
                this.detailsLoading = false;
            })
            return result.message;
        }

        runInAction(() => {
            this.detailsLoading = false;
        });

        return false;
    }
    // 设置安全码输入的可见性
    @action public setVisible = (visible = false) => {
        this.modalVisible = visible;
    }
}

export default new UserList();
